package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;
    //查询所有角色信息
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }
    //添加角色信息
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, String[] permissionIds, String[] menuIds) {
        try {
            roleService.add(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }
    //根据ID查询角色信息
    @RequestMapping("/findById")
    public Result findById(Integer roleId) {
        try {
            Role role=roleService.findById(roleId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //根据角色ID查询所有权限ID
    @RequestMapping("/findpermissionIdsByroleId")
    public Result findpermissionIdsByroleId(Integer roleId){
        try{
            List<Permission> list=roleService.findpermissionIdsByroleId(roleId);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS,list);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    //根据角色ID查询所有菜单ID
    @RequestMapping("/findmenuIdsByroleId")
    public Result findmenuIdsByroleId(Integer roleId){
        try{
            List<Menu> list=roleService.findmenuIdsByroleId(roleId);
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, String[] permissionIds, String[] menuIds){
        try{
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            return  new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (Exception e){
            return  new Result(false,MessageConstant.DELETE_ROLE_FILE);
        }

    }

}